﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MODEL;

namespace StudentManage
{
    public partial class Frm_WorkAdd_Update : Form
    {
        public Frm_WorkAdd_Update()
        {
            InitializeComponent();
        }
        DataGridView dgv1;
        public Frm_WorkAdd_Update(DataGridView dgv1)
        {
            this.dgv1 = dgv1;
            InitializeComponent();
        }

        private void Frm_WorkAdd_Update_Load(object sender, EventArgs e)
        {

            //this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm";
            if (this.Text == "修改考勤记录")
            {
                this.button1.Text = "修改";
                this.textBox1.Text = dgv1.SelectedRows[0].Cells["W_StuCardId"].Value.ToString();
                this.dateTimePicker1.Text = dgv1.SelectedRows[0].Cells["W_Date"].Value.ToString();

                dateTimePicker2.Text = dgv1.SelectedRows[0].Cells["W_Date2"].Value.ToString();

                if (dgv1.SelectedRows[0].Cells["W_State"].Selected == true)
                {
                    this.checkBox1.Checked = true;
                }
                else
                {
                    this.checkBox1.Checked = false;
                }
                this.textBox4.Text = dgv1.SelectedRows[0].Cells["W_BeiZhu"].Value.ToString();
                this.dateTimePicker1.CustomFormat = dgv1.SelectedRows[0].Cells["W_Date"].Value.ToString();
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            Work work = new Work();
            work.W_StuCardId = textBox1.Text;
            work.W_Date = dateTimePicker1.Value.ToString();
            work.W_Date2 = dateTimePicker2.Value.ToString();
            work.W_State = checkBox1.Checked;
            work.W_BeiZhu = textBox4.Text;
            try
            {
                //执行添加
                if (button1.Text == "添加")
                {
                    int num = BLL.WorkManage.WorkAdd(work);
                    if (num > 0)
                    {
                        MessageBox.Show("操作成功");
                        this.Close();
                        this.dgv1.DataSource = BLL.WorkManage.ChaXunClass();
                        //Frm_List.listBox1.Items.Add($"添加卡号为：{dgv1.SelectedRows[0].Cells["W_StuCardId"].Value.ToString()} 的考勤记录，操作成功");
                    }
                    else
                    {
                        MessageBox.Show("操作失败");
                        //Frm_List.listBox1.Items.Add($"添加卡号为：{dgv1.SelectedRows[0].Cells["W_StuCardId"].Value.ToString()} 的考勤记录，操作失败");
                    }
                }
                //执行修改
                else if (button1.Text == "修改")
                {
                    work.W_ID = int.Parse(dgv1.SelectedRows[0].Cells["W_ID"].Value.ToString());

                    int num = BLL.WorkManage.WorkUpdate(work);
                    if (num > 0)
                    {
                        MessageBox.Show("操作成功");
                        this.Close();
                        this.dgv1.DataSource = BLL.WorkManage.ChaXunClass();
                        //Frm_List.listBox1.Items.Add("修改考勤规则成功");
                    }
                    else
                    {
                        MessageBox.Show("操作失败");
                        //Frm_List.listBox1.Items.Add("修改考勤规则失败");
                    }
                }

            }
            catch (Exception ex)
            {
                //显示错误信息
                MessageBox.Show(ex.Message);
                //Frm_List.listBox1.Items.Add("执行操作时出现问题:\t\n" + ex.Message);
            }
        }

        private void btn_Ka_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[12];
            if (HPITRFID.EPCSDKHelper.IdentifySingleTag(Frm_Main.handle, data, null, 0) == true)
            {
                MessageBox.Show("读取成功");
                // 将结果展示到文本框中
                this.textBox1.Text = HPITRFID.ConvertHelper.ByteArrayToHexString(data);
            }
            else 
            {
                MessageBox.Show("读取失败");
            }
        }
    }
}
